<?php

/**
 * 将返回的数据集转换成树
 * @param  array   $list  数据集
 * @param  string  $id    主键
 * @param  string  $pid   父节点名称
 * @param  string  $child 子节点名称
 * @param  integer $root  根节点ID
 * @return array          转换后的树
 */
function list_to_tree($list, $id = 'id', $pid = 'pid', $children = 'children', $root=0) {
    $tree = array();// 创建Tree
    if(is_array($list)) {
        // 创建基于主键的数组引用
        $refer = array();
        foreach ($list as $key => $value) {
            $refer[$value[$id]] =& $list[$key];
        }
        
        foreach ($list as $key => $value) {
            // 判断是否存在parent
            $parentId = $value[$pid];
            if ($root == $parentId) {
                $tree[$value[$id]] =& $list[$key];
            }else{
                if (isset($refer[$parentId])) {
                    $parent =& $refer[$parentId];
                    $parent[$children][$list[$key][$id]] =& $list[$key];
                }
            }
        }
    }
    return $tree;
}

/**
 * 将返回的数据集转换成瀑布数据
 * @param  array   $list  数据集
 * @return array          转换后的瀑布树
 */
function list_to_treefalls($tree,$id = 'id', $pid = 'pid',$children = 'children',$level=1,$cls='') {
	$treefalls = array();
	foreach ($tree as $key => $value) {
		if(isset($value[$children])){
			$treesub = $value[$children];
			unset($tree[$key][$children]);
			$tree[$key]['level']= $level;
			$tree[$key]['cls']= 'pid_'.$tree[$key][$pid].' '.$cls;
			$treefalls[$key]=&$tree[$key];
			$treefalls = $treefalls+list_to_treefalls($treesub,$id,$pid,$children,$level+1,$tree[$key]['cls']);
		}else{
			$tree[$key]['level']= $level;
			$tree[$key]['cls']= 'pid_'.$tree[$key][$pid].' '.$cls;
			$treefalls[$key]=&$tree[$key];
		}
	}
	return $treefalls;
}
/**
 * 遍历id下的孩子id列表
 * @param unknown_type $id_search
 * @param unknown_type $tree 
 * @param unknown_type $id tag id
 * @param unknown_type $pid tag pid
 * @param unknown_type $children tag children
 */
function get_childrenkeys($tree_node,$id = 'id', $pid = 'pid',$children = 'children'){
	$id_list = array();
	if(isset($tree_node[$children])){
		foreach($tree_node[$children] as $key=>$value){
			$id_list[$key]=$key;
			$id_list = $id_list+get_childrenkeys($value);
		}
	}
	return $id_list;
	
	
}

function fetch_array_value($input,$key){
	if(is_array($input)){
		return $input[$key];
	}else{
		return null;
	}
}

function photo_url_path($path){
	return C('DOMAIN_PHOTO').$path;
}
function photo_real_path($path,$isMkdir){
	$path =   C('REALPATH_PHOTO').$path;
		if(!is_dir($path) && isMkdir){
			if(!mkdir($path,0777,true)){
				$path='';
			}
		}
	return $path;
}

function get_productcate_code1($code){
	return substr($code, 0,2);
}
function is_productcate_code2($code){
	if(strlen($code)==4){
		return true;
	}else{
		return false;
	}
}
//生成订单号
function gen_order_code(){
	$mod = M('PublicSeq');
	$lastId = $mod->add(array('crt_time'=>time()));
	if($lastId){
		$lastId = date('ymd').$lastId;
	}else{
		$lastId = time();
	}
	return $lastId;
	
}
//文件缓存，可指定特定的文件夹子
function F_CACHE($name, $value='', $path=''){
	return F($name, $value, $path);
}

?>